package com.yc.vcode.web;

import com.yc.vcode.bean.VcodeProperties;
import com.yc.vcode.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
public class VcodeDo {
    @Autowired
    VcodeProperties properties;
    @GetMapping("config")
    VcodeProperties properties(){
        return properties;
    }
    @RequestMapping(value = "image",produces = MediaType.IMAGE_PNG_VALUE)
    public void image(HttpServletResponse response,
                      HttpSession session) throws IOException {
        //生成验证码图片返回浏览器
        String vcode= VerifyCodeUtils.outputImage(response);
        System.out.println(vcode);
        session.setAttribute("vcode",vcode);
        //设置超时时间
        long time=System.currentTimeMillis();
        time+=properties.getTimeout()*1000;
        session.setAttribute("timeOut",time);
    }
}
